package com.jzy.stock.kufangkuqu.controller;

import com.jzy.stock.kufangkuqu.model.AddVo;
import com.jzy.stock.kufangkuqu.model.SelectVo;
import com.jzy.stock.kufangkuqu.model.UpdateStatusVo;
import com.jzy.stock.kufangkuqu.model.UpdateVo;
import com.jzy.stock.kufangkuqu.service.KuFangKuQuService;
import com.jzy.stock.util.ResultBuildVo;
import com.jzy.stock.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by JZZK on 2022/7/11.
 */
@RestController
@CrossOrigin
public class KuFangKuQuController {

    @Autowired
    private KuFangKuQuService kuFangKuQuService;

    //分页查询
    @PostMapping("storageSection/queryPage")
    public ResultVo queryPage(@RequestBody SelectVo selectVo) {
        return kuFangKuQuService.queryPage(selectVo);
    }

    //按钮修改小类状态
    @PostMapping("storageSection/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid UpdateStatusVo UpdateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return kuFangKuQuService.updateStatus(UpdateStatusVo);
    }

    //新增
    @PostMapping("storageSection/add")
    public ResultVo add(@RequestBody @Valid AddVo addVo, BindingResult bindingResult) {
        // 添加需要表单验证，如果验证不通过，直接把错误信息传给前端
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = kuFangKuQuService.add(addVo);
        return resultVo;
    }

    //修改
    @PostMapping("storageSection/update")
    public ResultVo update(@RequestBody @Valid UpdateVo updateVo, BindingResult bindingResult) {
        // 修改需要表单验证，如果验证不通过，直接把错误信息传给前端
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = kuFangKuQuService.update(updateVo);
        return resultVo;
    }

    //新增库房下拉框
    @GetMapping("storageSection/queryWareHouse")
    public ResultVo queryWareHouse1(){
        return kuFangKuQuService.queryWareHouse();
    }

    //新增库区业务属性下拉框
    @GetMapping("storageSection/queryBusinessAttribute")
    public ResultVo queryBusinessAttribute(){
        return kuFangKuQuService.queryBusinessAttribute();
    }

    //点击查看
    @GetMapping("storageSection/queryStorageSectionById")
    public ResultVo queryStorageSectionById(Integer id){
        return kuFangKuQuService.queryStorageSectionById(id);
    }

}
